package com.novax.ex.activity.provider.service;

import com.novax.ex.activity.infrastructure.entity.MallProduct;
import com.novax.ex.activity.infrastructure.entity.MallProductMemos;
import com.novax.ex.activity.infrastructure.mapper.MallProductMapper;
import com.novax.ex.activity.infrastructure.mapper.MallProductMemosMapper;
import com.novax.ex.common.base.BaseMapper;
import com.novax.ex.common.base.BaseService;
import com.novax.ex.common.enums.MallOrderWinnerMemosStatus;
import com.novax.ex.common.enums.MallProductMemosOperation;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.math.BigDecimal;

/**
 * Description: 商品备注
 *
 * @author Deucalion@novax.email
 * @date 2023/3/29 16:32
 */
@Service
public class MallProductMemosService extends BaseService {
    @Resource
    private MallProductMemosMapper mallProductMemosMapper;
    @Resource
    private MallProductMapper mallProductMapper;
    @Override
    protected BaseMapper getBaseMapper() {
        return mallProductMemosMapper;
    }

    /** 新增备注 */
    @Transactional
    public boolean memoAdd(Long adminId, Long mallProductId, String memo) {
        // 商品信息
        MallProduct mallProduct = new MallProduct();
        mallProduct.setId(mallProductId);
        mallProduct.setMemo(memo);
        mallProduct.setUpdater(adminId);
        int product = mallProductMapper.updateByPrimaryKeySelective(mallProduct);
        // 商品备注信息
        MallProductMemos mallProductMemos = new MallProductMemos();
        mallProductMemos.setMallProductId(mallProductId);
        mallProductMemos.setMemo(memo);
        mallProductMemos.setCreator(adminId);
        mallProductMemos.setUpdater(adminId);
        mallProductMemos.setOperationType(MallOrderWinnerMemosStatus.artificial.getStatus());
        int productMemo = mallProductMemosMapper.insertSelective(mallProductMemos);
        return product > 0 && productMemo > 0;
    }

    /** 修改价格时新增备注 */
    public boolean changePrice(
            Long mallProductId, Long adminId, BigDecimal oldPrice, BigDecimal newPrice,Integer operationType) {
        // 商品备注信息
        MallProductMemos mallProductMemos = new MallProductMemos();
        mallProductMemos.setMallProductId(mallProductId);
        mallProductMemos.setCreator(adminId);
        mallProductMemos.setUpdater(adminId);
        mallProductMemos.setOldPrice(oldPrice);
        mallProductMemos.setNewPrice(newPrice);
        mallProductMemos.setOperationType(operationType);
        int productMemo = mallProductMemosMapper.insertSelective(mallProductMemos);
        return productMemo > 0;
    }
}
